<?php
//Custom styler module
class ThemexStyler {

	public static $data;
	public static $id=__CLASS__;
	
	//Init module
	public static function init() {
	
		self::$data=ThemexCore::$components['custom_styles'];
		
		//add custom scripts action
		add_action('wp_head', array(__CLASS__,'renderData'));
	
	}
	
	//Apply custom styles
	public static function renderData() {
	
		global $post;
	
		//favicon
		$out='<link rel="shortcut icon" href="'.ThemexCore::getOption('favicon',THEME_URI.'framework/admin/images/favicon.ico').'" />';
		
		//styles
		$out.='<style type="text/css">';
		
		if(is_array(self::$data)) {
			
			if($post->post_type=='page') {
				$page_background=get_post_meta($post->ID,'_page_background',true);
			}
		
			foreach(self::$data as $style) {
				$out.=$style['elements'].'{';
				
				if(is_array($style['attributes'])) {
					foreach($style['attributes'] as $attr_name=>$option_id) {					
						if(ThemexCore::getOption($option_id)) {
						
							if($attr_name=='background-image') {
								$background=$page_background!=''?$page_background:ThemexCore::getOption($option_id);
								$option='url('.$background.')';
							} else if($attr_name=='font-size') {
								$option=ThemexCore::getOption($option_id).'px';
							} else if($attr_name=='font-family') {
								$option=ThemexCore::getOption($option_id).', Arial, Helvetica, sans-serif';
							} else {
								$option=ThemexCore::getOption($option_id);
							}
							
							$out.=$attr_name.':'.$option.';';
						}						
					}
				}
				
				
				
				$out.='}';
			}
		}
		
		$out.=ThemexCore::getOption('css').'</style>';
		
		//fonts
		$fonts=array();
		if(ThemexCore::getOption('heading_font','Signika, sans-serif')=='Signika, sans-serif') {
			$fonts[]='"Signika:400,600"';
		} else {
			$fonts[]='"'.ThemexCore::getOption('heading_font').'"';
		}
		if(ThemexCore::getOption('content_font','Open Sans, sans-serif')=='Open Sans, sans-serif') {
			$fonts[]='"Open Sans:400,400italic,600"';
		}
		
		$out.='<script type="text/javascript">
		WebFontConfig = {google: { families: [ '.implode($fonts,',').' ] } };
		(function() {
			var wf = document.createElement("script");
			wf.src = ("https:" == document.location.protocol ? "https" : "http") + "://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js";
			wf.type = "text/javascript";
			wf.async = "true";
			var s = document.getElementsByTagName("script")[0];
			s.parentNode.insertBefore(wf, s);
		})();
		</script>';
		
		echo $out;
	}
	
}
?>